<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="org.json.*"%>
<%@ page import="com.gzgi.framework.*"%>
<%@ page import="com.gzgi.framework.el.*"%>
<%@ page import="com.gzgi.framework.util.*"%>
<%@ page import="com.gzgi.framework.config.*"%>
<%@ page import="com.gzgi.framework.context.*"%>
<%@ page import="com.gzgi.framework.tree.*"%>
<%@ page import="com.gzgi.framework.dataitem.*"%>
<%@ page import="com.gzgi.framework.sys.*"%>
<%@ page import="com.gzgi.framework.sys.model.*"%>
<%@ page import="com.gzgi.framework.resource.*"%>
<%@ page import="com.gzgi.workflow.jbpm.model.*"%>
<%@ page import="com.gzgi.workflow.jbpm.container.*"%>
<%@ page import="com.gzgi.form.service.*"%>
<%@ page import="com.gzgi.form.graph.def.*"%>
<%@ page import="com.gzgi.form.graph.exe.*"%>
<%@ page import="com.gzgi.form.renderer.*"%>
<%@ page import="com.gzgi.form.integration.*"%>
<%@ page import="org.apache.commons.lang.*"%>
<%
     String actorId = (String) request.getSession().getAttribute(
				SecurityContext.LOGIN_ACTOR_ID);
	 String app_name = request.getParameter("app_name");
	 String rowId = request.getParameter("rowId"); 	 
 	 if(rowId==null || app_name == null) {
		 return;
	 }
	 FormModel formModel = null;
	 FormService formService = null;
	 FormIntegrationService formIntegrationService = null;
	 ResourceService resourceService = null;
	 FormDefinition formDefinition = null;
	 FormApplication formApplication = null;
	 Collection dataFiles =  null; 
	 String content = null;
     try{
		 formService = (FormService) ContextFactory.getBean("formService");
		 formIntegrationService = (FormIntegrationService) ContextFactory
				.getBean("formIntegrationService");
		 resourceService = (ResourceService) ContextFactory
				.getBean("resourceService");
		 FormContext formContext = new FormContext();
		 formContext.setContextPath(request.getContextPath());
		 formApplication = formService.getFormApplicationByName(formContext, app_name);
         if(formApplication != null && StringUtils.isNotEmpty(formApplication.getFormName())){
			formContext.setFormApplication(formApplication);
			formDefinition = formService.getLatestFormDefinition(formContext, formApplication.getFormName());
			formContext.setFormDefinition(formDefinition);
			ProcessContainer container = ProcessContainer.getContainer();
			String processDefinitionId = null;
            String processInstanceId = formContext.getString("processInstanceId");
	  	    String taskInstanceId = formContext.getString("taskInstanceId");
		    String taskName = formContext.getString("taskName");
			if (StringUtils.isNotEmpty(taskInstanceId)
					&& StringUtils.isNumeric(taskInstanceId)) {
				TaskItem taskItem = container.getTaskItem(taskInstanceId);
				if (taskItem != null) {
					processDefinitionId = taskItem.getProcessName();
					taskName = taskItem.getTaskName();
				}
			} else if (StringUtils.isNotEmpty(processInstanceId)
					&& StringUtils.isNumeric(processInstanceId)) {
				TaskItem taskItem = container.getMinTaskItem(formContext
						.getActorId(), processInstanceId);
				if (taskItem != null) {
					processDefinitionId = taskItem.getProcessName();
					taskName = taskItem.getTaskName();
				}
			}

			if (StringUtils.isNotEmpty(processDefinitionId)
					&& StringUtils.isNotEmpty(taskName)) {
				AccessEntry accessEntry = formIntegrationService.getAccessEntry(
						formContext, formApplication.getName(),
						processDefinitionId, taskName);
				if (accessEntry == null) {
					accessEntry = formIntegrationService.getLatestAccessEntry(
							formContext, formApplication.getName(), formApplication
									.getProcessName(), taskName);
				}
				if (accessEntry != null
						&& StringUtils.isNotEmpty(accessEntry.getFormName())) {
				}
			}	

			Writer writer = (Writer) CommandFactory.execute(formContext, "renderForm");
			if (writer != null) {
				 content = writer.toString();				
			}
		 }
		 }catch(Exception ex){
		  ex.printStackTrace();
	  }

%>
     
	<%=content != null ? content : ""%>
